package com.ltu.util.common;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;


public class IdsStrToListUtils {

	/**
	 * 字符串id转为List工具类
	 * 
	 */
	public static List<Integer> getInByString(String ids){

		List<Integer> arrayList = new ArrayList<Integer>();
		if (!StrUtils.isTrimNull(ids)){
			String[] idArry = ids.split(",");
			for (int i = 0; i < idArry.length; i++) {
				if(StringUtils.isBlank( idArry[i]) || !isNumeric(idArry[i])) 
					continue;
				arrayList.add(Integer.valueOf(idArry[i]));
			}
		}
		return arrayList;
	}

	public static List<Long> getLongByString(String ids){
		List<Long> arrayList = new ArrayList<Long>();
		if (!StrUtils.isTrimNull(ids)){
			String[] idArry = ids.split(",");
			
			for (int i = 0; i < idArry.length; i++) {
				if (Long.valueOf(idArry[i])!=0) {
					arrayList.add(Long.valueOf(idArry[i]));
				}
			}
		}
		
		return arrayList;
	}

	public static List<String> getStringList(String ids){
		List<String> arrayList = new ArrayList<String>();
		if (!ids.isEmpty()) {
			String[] idArry = ids.split(",");

			for (int i = 0; i < idArry.length; i++) {
				if (!StrUtils.isTrimNull(idArry[i])) {
					arrayList.add(idArry[i]);
				}
			}
		}
		return arrayList;
	}
	public static String listToString(List<String> list, char separator) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			sb.append(list.get(i)).append(separator);
		}
		return sb.toString().substring(0, sb.toString().length() - 1);
	}
	public static String listToString2(List<Long> list, char separator) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			sb.append(list.get(i)).append(separator);
		}
		return sb.toString().substring(0, sb.toString().length() - 1);
	}
	public static String listToString3(List<Integer> list, char separator) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			sb.append(list.get(i)).append(separator);
		}
		return sb.toString().substring(0, sb.toString().length() - 1);
	}
	/**
	 * 判断id 是否在集合ids里面
	 */
	public static Boolean checkStrInList(List<Integer> ids,Integer id){
		if (ids.size()==0)
			return true;
		for (int i=0;i<ids.size();i++){
			if (ids.get(i).intValue()==id)
				return true;
		}
		return false;
	}

	/**
	 * 判断id 是否在集合ids里面
	 */
	public static Boolean checkStrInList(List<String> ids,String id){
		if (ids.size()==0)
			return true;
		for (int i=0;i<ids.size();i++){
			if (ids.get(i).equals(id))
				return false;
		}
		return true;
	}


	public static void main(String[] args) {

	}
	
	//判断输入的不是字符串
    public static boolean isNumeric(String str) {
        // 该正则表达式可以匹配所有的数字 包括负数
//        Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");//小数合法
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");//小数不合法
        String bigStr;
        try {
            bigStr = new BigDecimal(str).toString();
        } catch (Exception e) {
            return false;//异常 说明包含非数字。
        }

        Matcher isNum = pattern.matcher(bigStr); // matcher是全匹配
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }
    //过滤特殊字符串
    public static String StringFilter(String str) { 
    	// 只允许字母和数字 // String regEx ="[^a-zA-Z0-9]"; 
    	// 清除掉所有特殊字符 
    	String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]"; 
    	Pattern p = Pattern.compile(regEx); 
    	Matcher m = p.matcher(str);
    	return m.replaceAll("").trim();
    }

	/**
	 * 删除item
	 * @param mainStr
	 * @param itemStr
	 * @return
	 */
    public static String removeItem(String mainStr, String itemStr) {
		mainStr = mainStr.replace(itemStr, "");
		mainStr = mainStr.replace(",,,", ",");
		mainStr = mainStr.replace(",,", ",");
		String isEmpty = mainStr.replace(",", "");
		if (StrUtils.isTrimNull(isEmpty))
			mainStr = "";
		return mainStr;
	}
}